package com.example.day8_shareprefrences_sdcard;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 项目中sp工具类的使用：单例模式
 */
public class SPUtils {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    //单例模式
    private static SPUtils spUtils;
    private SPUtils(Context context){
        sharedPreferences = context.getSharedPreferences("yao",Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }
    public synchronized static SPUtils getInstance(Context context){
        if(spUtils == null){
            spUtils = new SPUtils(context);
        }
        return spUtils;
    }
    //存
    public void putString(String key,String value){
        editor.putString(key,value);
        editor.commit();
    }
    //取
    public String  getString(String key){
        return  sharedPreferences.getString(key,"");
    }
    //清空
    public void clear(){
        editor.clear();
        editor.commit();
    }
}
